//
//  ViewController.m
//  CopyAndMutableCopy-Demo
//
//  Created by xt on 2019/1/21.
//  Copyright © 2019 TJ. All rights reserved.
//
/*
 mutable 英['mjuːtəb(ə)l] adj. 易变的，不定的；性情不定的
 container 英[kən'teɪnə] n. 集装箱；容器
 array 英[ə'reɪ] n. 数组，阵列；排列，列阵；大批，一系列；衣服
 dictionary 英['dɪkʃ(ə)n(ə)rɪ] n. 字典；词典
 */

/*
 * 参考资料链接:https://www.jianshu.com/p/e98e6e30ebda
 */
/*
 * 需要遵守的协议和实现的协议方法
 *
 * copy:浅拷贝(指针拷贝)
 * <NSCopying>
 * - (id)copyWithZone:(nullable NSZone *)zone;
 * 1.拷贝出来的是不可变对象
 *
 *
 * mutableCopy:深拷贝(内容拷贝)
 * <NSMutableCopying>
 * - (id)mutableCopyWithZone:(nullable NSZone *)zone;
 * 1.拷贝出来的是可变对象
 *
 */

#import "ViewController.h"
#import "copyClass.h"
#import "mutableCopyClass.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
//    [self systemNonContainerClass];
//    [self systemContainerClass];
    [self customClassCopy];
    [self customClassMutableCopy];
}

/*
 * 系统提供的非容器类:NSString,NSMutableString
 *
 *              copy           mutableCopy
 * 不可变对象     浅拷贝          深拷贝
 * 可变对象       深拷贝          深拷贝
 */
- (void)systemNonContainerClass
{
    NSString *str = @"copy";
    NSString *str_copy = [str copy];
    NSMutableString *str_mutableCopy = [str mutableCopy];
    NSLog(@"\n内存地址:str = %p, str_copy = %p, str_mutableCopy = %p", str, str_copy, str_mutableCopy);
    
    
    NSMutableString *mutableStr = [NSMutableString stringWithString:@"mutableCopy"];
    NSString *mutableStr_copy = [mutableStr copy];
    NSMutableString *mutableStr_mutableCopy = [mutableStr mutableCopy];
    NSLog(@"\n内存地址:mutableStr = %p, mutableStr_copy = %p, mutableStr_mutableCopy = %p", mutableStr, mutableStr_copy, mutableStr_mutableCopy);
}

/*
 * 系统提供的容器类:NSArray,NSDictionary
 *
 *              copy             mutablecopy
 * 不可变对象     浅拷贝            深拷贝
 * 可变对象       单层内容深拷贝     单层内容深拷贝
 */
- (void)systemContainerClass
{
    NSArray *array = [NSArray arrayWithObjects:@"1", nil];
    NSArray *array_copy = [array copy];
    NSMutableArray *array_mutableCopy = [array mutableCopy];
    NSLog(@"\n内存地址:array = %p, array_copy = %p, array_mutableCopy = %p", array, array_copy, array_mutableCopy);
    
    NSMutableArray *mutableArray = [NSMutableArray arrayWithObject:@1];
    NSMutableArray *mutableArray1 = [NSMutableArray arrayWithObject:mutableArray];
    NSArray *arrayCopy = [mutableArray1 copy];
    NSMutableArray *arrayMutableCopy = [mutableArray1 mutableCopy];
    
    //单层的值用不同的存储地址,+1层的值指向同一地址的指针地址
    [arrayMutableCopy[0] addObject:@2];
    [arrayMutableCopy addObject:@3];
    
    NSLog(@"\n内存地址:mutableArray1 = %p, arrayCopy = %p, arrayMutableCopy = %p", mutableArray1, arrayCopy, arrayMutableCopy);
    NSLog(@"\n内容:mutableArray1 = %@, arrayCopy = %@, arrayMutableCopy = %@", mutableArray1, arrayCopy, arrayMutableCopy);
}

/*
 * 自定义的类
 * 不可变对象用copy
 * copy:都是深拷贝
 */
- (void)customClassCopy
{
    copyClass *class1 = [[copyClass alloc] init];
    class1.str = @"class1";
    class1.array = @[@"class1"];
    class1.dict = @{@"class1":@"1"};
    
    copyClass *class2 = [class1 copy];
    class2.str = @"class2";
    class2.array = @[@"class2"];
    class2.dict = @{@"class2":@"2"};
    
    NSLog(@"\n内存地址:class1 = %p, class2 = %p", class1, class2);
    NSLog(@"\n内容:class1.str = %@, class2.str = %@\nclass1.array = %@, class2.array = %@\nclass1.dict = %@, class2.dict = %@", class1.str, class2.str, class1.array, class2.array, class1.dict, class2.dict);
}

/*
 * 可变对象用mutableCopy
 * mutableCopy:都是深拷贝
 */
- (void)customClassMutableCopy
{
    mutableCopyClass *class1 = [[mutableCopyClass alloc] init];
    class1.mutableStr = [NSMutableString stringWithString:@"class1"];
    class1.mutableArray = [NSMutableArray arrayWithArray:@[@"class1"]];
    class1.mutableDict = [NSMutableDictionary dictionaryWithDictionary:@{@"class1":@"1"}];
    
    mutableCopyClass *class2 = [class1 mutableCopy];
    class2.mutableStr = [NSMutableString stringWithString:@"class2"];
    class2.mutableArray = [NSMutableArray arrayWithArray:@[@"class2"]];
    class2.mutableDict = [NSMutableDictionary dictionaryWithDictionary:@{@"class2":@"2"}];
    
    NSLog(@"\n内存地址:class1 = %p, class2 = %p", class1, class2);
    NSLog(@"\n内容:class1.str = %@, class2.str = %@\nclass1.array = %@, class2.array = %@\nclass1.dict = %@, class2.dict = %@", class1.mutableStr, class2.mutableStr, class1.mutableArray, class2.mutableArray, class1.mutableDict, class2.mutableDict);
}

@end
